package org.eenie.dagger2.ui.main;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.eenie.dagger2.R;
import org.eenie.dagger2.base.BaseActivity;
import org.eenie.dagger2.entity.Article;
import org.eenie.dagger2.utils.LogUtil;
import org.greenrobot.greendao.rx.RxDao;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity implements MainContract.View {

    @Inject
    RxDao<Article, Long> mRxArticleDao;
    @Inject
    MainPresenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaggerMainComponent.builder()
                .appComponent(getAppComponent())
                .build()
                .inject(this);


        TextView tvId = (TextView) findViewById(R.id.tv_id);

        findViewById(R.id.btn_insert).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        findViewById(R.id.btn_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        findViewById(R.id.btn_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });

    }


    private void insert() {
        Article article = new Article(null, "title", "content");
        mRxArticleDao.insert(article)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Article>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Article article) {

                        LogUtil.e("insert new article id = " + article.getId());

                    }
                });
    }


    @Override
    public void showContent() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showError() {

    }
}
